package controller;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import view.HandComponent;
import view.StackFragment;
import view.TableComponent;

import model.CardStack;
import model.GameModel;

class RightClickListener extends MouseAdapter 
{
	TableComponent table;
	
	public RightClickListener(TableComponent table)
	{
		this.table = table;
	}
	
	
    public void mousePressed(MouseEvent e){
        if (e.isPopupTrigger() && e.getButton() == 3)
            makeRightClickMenu(e);
    }

    public void mouseReleased(MouseEvent e){
        if (e.isPopupTrigger() && e.getButton() == 3)
            makeRightClickMenu(e);
    }

    private void makeRightClickMenu(MouseEvent e){
    	
        int mx = e.getXOnScreen() - table.getLocationOnScreen().x;
        int my = e.getYOnScreen() - table.getLocationOnScreen().y;
        
        StackFragment si = table.getSelectionAt(mx, my);
        if(si==null) return;
        if(table.getSelection()==null || !table.getSelection().containsFragment(si.stack))
        	MetaDeckController.getInstance().getGame().setTableSelection(new StackFragment(si.stack, si.stack.size()));

        RightClickMenu menu = new RightClickMenu(table);

        menu.show(e.getComponent(), e.getX(), e.getY()); 
    }
}